using System;
using ThirdLife.UI.WinForms.Controls;

namespace ThirdLife.UI.WinForms.Modules
{
  public class SimpleMetaverseConnectionUIModule : IUIModule, ISimpleGridConnectionController
  {
    #region IUIModule Members

    private IUIModuleServices moduleServices;

    public IUIModuleServices ModuleServices
    {
      get { return moduleServices; }
      set { moduleServices = value; }
    }

    public string Name
    {
      get { return "ThirdLife_SimpleMetaverseConnection"; }
    }

    public void Setup()
    {
      ModuleServices.Startup.ApplicationStarted += Startup_ApplicationStarted;
    }

    #endregion

    private void Startup_ApplicationStarted()
    {
      ModuleServices.Registration.AddMenu("File", 0);
      //we probably always want this to be last
      ModuleServices.Registration.AddMenuItem("File", "Grid ...", 1, OnGridButtonPressed);
      ModuleServices.Navigation.RequestGridList += new GenericEventHandler(Navigation_RequestGridList);
    }

    void Navigation_RequestGridList()
    {
      ShowGrids();
    }

    private void OnGridButtonPressed(object sender, EventArgs e)
    {
      ShowGrids();
    }

    private void ShowGrids()
    {
      TLSimpleMetaverseConnection tsmc = new TLSimpleMetaverseConnection();
      tsmc.Controller = this as ISimpleGridConnectionController;
      tsmc.SetupView();
      tsmc.ShowWindow();
    }

    public void ConnectWithInfo(string username, string password, string url)
    {
      ModuleServices.Metaverse.ConnectToGridWithInfo(username, password, url);
    }

    public string GetDefaultURL()
    {
      return ModuleServices.Metaverse.DefaultURL;
    }

    public string GetDefaultPassword()
    {
      return ModuleServices.Metaverse.DefaultPassword;
    }

    public string GetDefaultName()
    {
      return ModuleServices.Metaverse.DefaultName;
    }
  }
}